#include "Test.Application.h"

#undef  APP_NAME
#define APP_NAME	TestLines
#undef  APP_BASE
#define APP_BASE	TestApp


class APP_NAME : public APP_BASE
{
protected:
	BatchMaterial	material;
	array< vec2 >	spline;

	PolyLine2D		polyLine;


public:
	APP_NAME (IPlatform *platform, EDevice::type deviceType) :
		APP_BASE( platform, deviceType )
	{}

	void Update (float dt) override
	{
		APP_BASE::Update( dt );

		ESS()->GetGraphicsEngine()->GetViewport()->Apply();
		ESS()->GetGLStateManager()->ClearColor( color4f( 0.0f ) );

		//gles::glLineWidth( 10.0f );
		//ESS()->GetGraphicsEngine()->GetRender2D().DrawSpline( material, spline, color4u(255), 0.02f );

		//ESS()->GetGraphicsEngine()->GetRender2D().DrawSpline( material, spline, color4u(255),
		//									rect(0.4f, 0.0f, 0.6f, 1.0f), 0.02f, 0.02f, 0.02f );
		
		ESS()->GetGraphicsEngine()->GetRender2D().DrawPolyLine( material, polyLine, color4u(255), 0.02f );
		
		//ESS()->GetGraphicsEngine()->GetRender2D().DrawPolyLine( material, polyLine, color4u(255),
		//										rect(0.0f, 0.0f, 1.0f, 1.0f), 0.02f, 0.04f, 0.04f );

		batch.Flush();
		batch.Clear();
		
		ESS()->GetGraphicsEngine()->SwapBuffers();
	}

	void OnEnter () override
	{
		APP_BASE::OnEnter();

		/*TexturePtr	tex;
		ESS()->GetResourceManager()->Load( EResource::TEXTURE, "tex/dot.tex", tex );

		material.SetTexture( 0, tex );*/

		ProgramPtr	program = ESS()->GetGraphicsEngine()->GetShaderManager()->
								GetDefaultProgram( EDefaultShader::COLORED );
		material.SetProgram( program );


		// build spline
		spline	<< vec2( 0.0f, 0.0f ) << vec2( 1.0f, 1.0f ) << vec2( 2.0f, 0.0f ) << vec2( 3.0f, 1.0f )
				<< vec2( 4.0f, 0.0f ) << vec2( 5.0f, 1.0f ) << vec2( 6.0f, 0.0f ) << vec2( 7.0f, 1.0f );

		vec2	scale( 0.3f, 0.5f );
		vec2	bias( -1.0f, 0.0f );

		FOR( i, spline ) {
			spline[i] *= scale;
			spline[i] += bias;
		}

		// build poly line
		/*
		polyLine.SetStartPoint( vec2( 0.0f, 0.0f ) )
				.AddArcWithRadius( vec2(0.2f, 0.0f), 0.3f );
		*/
		/*
		polyLine.SetStartPoint( vec2( 0.0f, 0.0f ) )
				.AddSmoothLine( vec2( 0.4f, 0.4f ), 1.0f )
				.AddSmoothLine( vec2( 0.8f, 0.0f ), 1.0f )
				.AddSmoothLine( vec2( 0.4f, -0.4f ), 0.5f )
				.AddSmoothLine( vec2( 0.0f, 0.0f ), 0.5f );
		*/
		
		polyLine.SetStartPoint( vec2( -0.9f, 0.0f ) )
				.AddLine( vec2( -0.7f, 0.5f ) )
				.AddArcWithHeight( vec2(-0.6f, 0.6f), -0.1f )
				.AddLine( vec2( -0.5f, 0.0f ) )
				.AddSpline( vec2( -0.3f, 0.5f ) )
				/*.AddSpline( vec2( -0.1f, 0.0f ) )
				.AddLine( vec2( 0.1f, 0.5f ) )
				.AddLine( vec2( 0.3f, 0.0f ) )*/;
		
	}

	void OnExit () override
	{
		material.Clear();
		APP_BASE::OnExit();
	}
};